package com.itcam.tingshu.user.receiver;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.itcam.tingshu.common.constant.KafkaConstant;
import com.itcam.tingshu.user.service.UserInfoService;
import com.itcam.tingshu.vo.user.UserPaidRecordVo;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author : Cammy.Wu
 * Description : 监听用户消息
 */

@Component
public class UserReceiver {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 监听用户支付记录
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_PAY_RECORD)
    public void userPayRecord(ConsumerRecord<String, String> record) {
        // 获取发送的数据
        String strJson = record.value();
        // 判断
        if (!StringUtils.isEmpty(strJson)) {
            // 数据类型转换
            UserPaidRecordVo userPaidRecordVo = JSON.parseObject(strJson, UserPaidRecordVo.class);
            // 调用服务层方法记录用户购买信息
            userInfoService.userPayRecord(userPaidRecordVo);
        }
    }
}
